package com.smedi.ismedi.inmail.api.dto.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知公告反参
 *
 * @author jiangqinxuan
 * @date 2021/07/23
 */
@Data
@ApiModel(value = "通知公告")
public class AnnouncementResponse implements Serializable {
    /**
     * id
     */
    @ApiModelProperty(value = "id")
    private Long id;
    /**
     * 标题
     */
    @ApiModelProperty(value = "标题")
    private String title;
    /**
     * 简介
     */
    @ApiModelProperty(value = "简介")
    private String content;
    /**
     * 通知公告发布人
     */
    @ApiModelProperty(value = "通知公告发布人")
    private PersonResponse publisher;
    /**
     * 通知公告接收人
     */
    @ApiModelProperty(value = "通知公告接收人")
    private List<ReceiverResponse> receivers = new ArrayList<>();
    /**
     * 审核人
     */
    @ApiModelProperty(value = "审核人")
    private PersonResponse reviewer;
    /**
     * 流程任务
     */
    @ApiModelProperty(value = "流程任务")
    private TaskResponse task;
    /**
     * 附件
     */
    @ApiModelProperty(value = "附件")
    private List<AttachmentResponse> attachments;
    /**
     * 发布时间
     */
    @ApiModelProperty(value = "发布时间")
    private LocalDateTime publishDateTime;
    /**
     * 审核时间
     */
    @ApiModelProperty(value = "审核时间")
    private LocalDateTime reviewDateTime;
    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createDateTime;
    /**
     * 是否限内部
     */
    @ApiModelProperty(value = "是否限内部")
    private Boolean internal;
    /**
     * 总体 总接收人数
     */
    @ApiModelProperty(value = "总体 总接收人数")
    private int receiverTotalNum;
    /**
     * 总体 已读接收人数
     */
    @ApiModelProperty(value = "总体 已读接收人数")
    private int readReceiverNum;
    /**
     * 总体 未读接收人数
     */
    @ApiModelProperty(value = "总体 未读接收人数")
    private int noReadReceiverNum;
    /**
     * 管理部门/主体院 总接收人数
     */
    @ApiModelProperty(value = "管理部门/主体院 总接收人数")
    private int receiverTotalNumInInstitutionAndOrganization;
    /**
     * 管理部门/主体院 已读接收人数
     */
    @ApiModelProperty(value = "管理部门/主体院 已读接收人数")
    private int readReceiverNumInInstitutionAndOrganization;
    /**
     * 管理部门/主体院 未读接收人数
     */
    @ApiModelProperty(value = "管理部门/主体院 未读接收人数")
    private int noReadReceiverNumInInstitutionAndOrganization;
    /**
     * 管理部门 总接收人数
     */
    @ApiModelProperty(value = "管理部门 总接收人数")
    private int receiverTotalNumInInstitution;
    /**
     * 管理部门 已读接收人数
     */
    @ApiModelProperty(value = "管理部门 已读接收人数")
    private int readReceiverNumInInstitution;
    /**
     * 管理部门 未读接收人数
     */
    @ApiModelProperty(value = "管理部门 未读接收人数")
    private int noReadReceiverNumInInstitution;
    /**
     * 主体院 总接收人数
     */
    @ApiModelProperty(value = "主体院 总接收人数")
    private int receiverTotalNumInOrganization;
    /**
     * 主体院 已读接收人数
     */
    @ApiModelProperty(value = "主体院 已读接收人数")
    private int readReceiverNumInOrganization;
    /**
     * 主体院 未读接收人数
     */
    @ApiModelProperty(value = "主体院 未读接收人数")
    private int noReadReceiverNumInOrganization;
    /**
     * 项目 总接收人数
     */
    @ApiModelProperty(value = "项目 总接收人数")
    private int receiverTotalNumInProject;
    /**
     * 项目 已读接收人数
     */
    @ApiModelProperty(value = "项目 已读接收人数")
    private int readReceiverNumInProject;
    /**
     * 项目 未读接收人数
     */
    @ApiModelProperty(value = "项目 未读接收人数")
    private int noReadReceiverNumInProject;
    /**
     * 管理部门/主体院统计
     */
    @ApiModelProperty(value = "管理部门/主体院统计")
    private List<StatisticsResponse> statisticsInInstitutionAndOrganization;
    /**
     * 管理部门统计
     */
    @ApiModelProperty(value = "管理部门统计")
    private List<StatisticsResponse> statisticsInInstitution;
    /**
     * 主体院统计
     */
    @ApiModelProperty(value = "主体院统计")
    private List<StatisticsResponse> statisticsInOrganization;
    /**
     * 项目统计
     */
    @ApiModelProperty(value = "项目统计")
    private List<StatisticsInProjectResponse> statisticsInProject;
    /**
     * 当前登录人是否已读
     */
    @ApiModelProperty(value = "当前登录人是否已读")
    private Boolean readByCurrentUser;
    /**
     * 选中节点
     */
    @ApiModelProperty(value = "选中节点")
    private List<String> nodeKeys;
    /**
     * 通知公告类型
     */
    @ApiModelProperty(value = "通知公告类型")
    private String type;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class StatisticsResponse {
        @ApiModelProperty(value = "统计 名称")
        private String name;
        @ApiModelProperty(value = "来源 id")
        private Long sourceFromId;
        @ApiModelProperty(value = "来源 类型")
        private String sourceFromType;
        @ApiModelProperty(value = "统计 接收人列表")
        private List<ReceiverResponse> receivers = new ArrayList<>();
        @ApiModelProperty(value = "统计 总数")
        private int receiverTotalNum;
        @ApiModelProperty(value = "统计 已读人数")
        private int readReceiverNum;
        @ApiModelProperty(value = "统计 未读人数")
        private int noReadReceiverNum;
        @ApiModelProperty(value = "流转单名称")
        private String readRecordName;


        public List<ReceiverResponse> getReceivers() {
            if (null == receivers || receivers.isEmpty()) {
                return new ArrayList<>();
            }
            return receivers.stream().sorted(Comparator.comparing(ReceiverResponse::getPersonOrder)).collect(Collectors.toList());
        }

        public String getReadRecordName() {
            return "文件流转单" + "_" + this.getName() + ".pdf";
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class StatisticsInProjectResponse {
        @ApiModelProperty(value = "统计 名称")
        private String name;
        @ApiModelProperty(value = "统计 项目统计列表")
        private List<StatisticsResponse> projectStatistics;
    }

    public List<String> getNodeKeys() {
        List<String> nodeKeys = new ArrayList<>();
        if (null == this.receivers) {
            return null;
        }
        for (ReceiverResponse receiver : this.receivers) {
            nodeKeys.add("PERSON" + "-" + receiver.getId());
        }
        return nodeKeys;
    }
}